package com.neuedu.mybatis.dao;

import com.neuedu.mybatis.entity.Dept;
import com.neuedu.mybatis.entity.UmsUser;
import com.neuedu.mybatis.util.SessionFactoryUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 项目：      mybatis
 * 类名:       DeptDao
 * 创建时间：  2024/3/5 15：34
 * 描述 :
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
public class DeptDao {

    //线程安全的 重计量  全局唯一
    SqlSessionFactory sqlSessionFactory = SessionFactoryUtils.getInstance();

    public DeptDao() throws IOException {
    }


    /**
     * SqlSession 用户接口
     * @throws IOException
     */

    /**
     * 查询集合
     * @return
     * @throws IOException
     */
    public  List  selectList() throws IOException {
        //  SqlSession session   线程不安全的，每个线程自己独享
        SqlSession session =sqlSessionFactory.openSession();
        List<Dept> result = new ArrayList<>();
        try{
            //执行
            result = session.selectList("dept.selectList");
        }catch (Exception e){
            e.printStackTrace();
        }

        session.close();

        return result;

    }


    public  long  selectCount() throws IOException {
        //  SqlSession session   线程不安全的，每个线程自己独享
        SqlSession session =sqlSessionFactory.openSession();
        long count = 0;
        try{
            //执行
            count = session.selectOne("dept.selectCount");
        }catch (Exception e){
            e.printStackTrace();
        }

        session.close();

        return count;

    }


    /**
     * 根据主键查询单条结果
     * @param id
     * @return
     * @throws IOException
     */
    public  Dept selectOne(long id ) throws IOException {

        //  SqlSession session   线程不安全的，每个线程自己独享
        SqlSession session =sqlSessionFactory.openSession();
        Dept dept = null;

        try{
            //执行
            dept  = session.selectOne("dept.selectOne",id);
        }catch (Exception e){
            e.printStackTrace();
        }
        session.close();
        return dept;
    }
    
    public  int insert(Dept dept) throws IOException {
        //  SqlSession session   线程不安全的，每个线程自己独享
        SqlSession session =sqlSessionFactory.openSession();
        //执行
        int count = 0;
        try {
            count = session.insert("dept.insert", dept);
            session.commit();
        }catch (Exception e){
            session.rollback();
            e.printStackTrace();
        }
        session.close();

        return  count;
    }

    public  int update(Dept dept) throws IOException {
        //  SqlSession session   线程不安全的，每个线程自己独享
        SqlSession session =sqlSessionFactory.openSession();
        //执行
        int count = 0;
        try {
            count = session.update("dept.update", dept);
            session.commit();
        }catch (Exception e){
            session.rollback();
            e.printStackTrace();
        }
        session.close();

        return  count;
    }

    public  int deleteById(long id) throws IOException {
        //  SqlSession session   线程不安全的，每个线程自己独享
        SqlSession session =sqlSessionFactory.openSession();
        //执行
        int count = 0;
        try {
            count = session.delete("dept.deleteById", id);
            session.commit();
        }catch (Exception e){
            session.rollback();
            e.printStackTrace();
        }
        session.close();

        return  count;
    }

}
